﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : Role
{
	#region 属性

	protected bool allowMove = true;

	protected int price;
	public int Price => price;          // 杀死得分


	protected EnemyType type;
	public EnemyType Type => type;
	#endregion


	/// <summary>
	/// 设置初始属性
	/// 每个怪物创建出来都要执行一次
	/// </summary>
	/// <param name="hp"></param>
	/// <param name="price"></param>
	/// <param name="attack"></param>
	public virtual void SetIniAttributes(int hp, int price, int attack, EnemyType type) {
		this.roleType = RoleType.Enemy;
		SetEnemyState(EnemyState.Idle, true);
		this.hp = hp;
		CurrentHp = hp;
		this.price = price;
		this.attack = attack;
		this.type = type;
	}


	/// <summary>
	/// 设置怪物状态
	/// </summary>
	/// <param name="state"></param>
	public virtual void SetEnemyState(EnemyState state, bool b) {
		ani.SetBool("Is" + state.ToString(), b);
	}

	public virtual void SetEnemyState(EnemyState state) {
		ani.SetTrigger("Is" + state.ToString());
	}




	public virtual void ReceivedAttack(int hurt) {
		CurrentHp -= hurt;
		// Debug.Log(type + "hp:" + CurrentHp);
		if (CurrentHp <= 0) {   // 死亡
			OnEnterEnemyDieEvent();
		} else {    // 未死亡
			SetEnemyState(EnemyState.Attack);
		}
	}



	public virtual void OnCollisionEnter2D(Collision2D other) {
		// Debug.Log("1111111" + other.gameObject.tag);

		// if (other.gameObject.tag == "Player") { // 与Player相撞
		// 	ReceivedAttack(1);
		// } else
	}

	public virtual void OnTriggerEnter2D(Collider2D other) {
		// Debug.Log("1111111" + other.gameObject.tag);
		if (other.gameObject.tag == "PlayerMelee") { // 受到近战攻击
			ReceivedAttack(1);
		} else if (other.gameObject.tag == "PlayerBullet") {   // 受到远距离伤害
			ReceivedAttack(2);
			other.gameObject.GetComponent<Bullet>().OnDestroyBullet();
		} else if (other.gameObject.tag == "Player") {  // 攻击到Player
			Debug.Log("受到攻击");
			other.GetComponent<Player>().RecvAttack(Attack);
		}
	}


	public virtual void OnEnterEnemyDieEvent() {
		allowMove = false;
		SetEnemyState(EnemyState.Die, true);
		Destroy(GetComponent<Collider2D>());
	}


	public virtual void OnExitEnemyDieEvent() {
		GameController.Instance.CntEnemy++;
		Destroy(this.gameObject);
	}




	/// <summary>
	/// 进入动画前事件
	/// </summary>
	/// <param name="state"></param>
	public virtual void OnEnterAniEvent(EnemyState state) {

	}

	/// <summary>
	/// 退出动画前事件
	/// </summary>
	/// <param name="state"></param>
	public virtual void OnExitAniEvent(EnemyState state) {

	}


}
